<template>
  <el-scrollbar wrap-style="border-radius: 10px !important;" :style="{ 'max-height': courseheight }" class="coursebox">
    <div class="innerbox" :style="{ 'min-height': courseheight }">
      <div class="m-t-30">
        <ul class="courselistul clearfix">
          <li class="courseitem" @click="openExamSum(item.id)" v-for="(item, index) in exammylist" :key="index">
            <div class="infobox">
              <div class="examtitle">{{ item.title }}</div>
              <div class="middlebox">
                <p class="examcourse">课程：{{ item.course_name }}</p>
                <el-tag type="success" effect="light" color="transparent">
                  {{ examstatus[item.status + 1] }}
                </el-tag>
              </div>
              <p class="duringtime">{{ item.begin_time }}~{{ item.end_time }}</p>
            </div>
          </li>

          <li class="courseitem" @click="openExamSum(666)">
            <div class="infobox">
              <div class="examtitle">高压电工基础考试</div>
              <div class="middlebox">
                <p class="examcourse">课程：高压电工课程</p>
                <el-tag type="success" effect="light" color="transparent">
                  进行中
                </el-tag>
              </div>
              <p class="duringtime">2021-12-22 13:54:21～2023-12-22 15:54:21</p>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </el-scrollbar>

  <el-dialog v-model="dialogExamVisible" width="800">
    <div class="studentinfo">
      <div class="name_class">
        <ul>
          <li>
            <div class="labelbox">姓名：</div>
            <div class="contentbox">luffygo</div>
          </li>
          <li>
            <div class="labelbox">班级：</div>
            <div class="contentbox">{{ examdetail.class_name }}</div>
          </li>
          <li>
            <div class="labelbox">课程：</div>
            <div class="contentbox">{{ examdetail.course_name }}</div>
          </li>
        </ul>
      </div>
      <div class="score">
        <p>成绩：</p>
        <!-- <p>成绩评定中</p> -->
        <p>80分</p>

      </div>
    </div>
    <div class="examinfo">
      <div>
        <ul>
          <li>
            <div class="labelbox">考试名称：</div>
            <div class="contentbox">{{ examdetail.title }}</div>
          </li>
          <li>
            <div class="labelbox">考试时间：</div>
            <div class="contentbox">{{ examdetail.begin_time }}~{{ examdetail.end_time }}</div>
          </li>
          <li>
            <div class="labelbox">考试限时：</div>
            <div class="contentbox">{{ examdetail.duration / 60 }}分钟</div>
          </li>
          <li>
            <div class="labelbox">考试总分：</div>
            <div class="contentbox">{{ examdetail.score_total }}</div>
          </li>
          <li>
            <div class="labelbox">合格分数：</div>
            <div class="contentbox">{{ examdetail.score }}</div>
          </li>
          <li>
            <div class="labelbox">题目数量：</div>
            <div class="contentbox">{{ examdetail.qnum }}</div>
          </li>
          <li>
            <div class="labelbox">题目类型：</div>
            <div class="contentbox">{{ examdetail.qtype }}</div>
          </li>
          <li>
            <div class="labelbox">考试状态：</div>
            <div class="contentbox">{{ examstatus[examdetail.status] }}</div>
          </li>
          <li>
            <div class="labelbox">完成状态：</div>
            <div class="contentbox">{{ userstatus[examdetail.user_status] }}</div>
          </li>
        </ul>
      </div>
      <div class="btnbox">
        <el-button type="primary" class="btnstyle" @click="gotoExamDetail">查询试卷详情</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted, inject } from 'vue'
import { useRouter } from 'vue-router'
import api from '@/request/api'

// 加载user模块 以及底下变量
import { storeToRefs } from 'pinia'
import useStore from '@/store'
const { user } = useStore()
let { username, avatar, uid } = storeToRefs(user)

// 初始化路由实例
const router = useRouter()
// 注入网络请求方法
const axios = inject('axios')

let courseheight = ref(0)
let exammylist = ref([])
let examdetail = ref({})
let examstatus = ref(['未开始', '进行中', '已结束'])
let userstatus = ref(['未完成', '已完成'])


let examId = ref(0)

// 模态框显示标识
const dialogExamVisible = ref(false)

onMounted(async () => {
  // 设置课程中间视图的最小高度
  courseheight.value = `${window.innerHeight - 60}px`

  // 获取我的考试列表（进行中）
  getmyExamlist()
})

// 获取课程
async function getmyExamlist() {
  let res = await axios.post(api.exammylist, {
    uid: uid.value,
    // key_id: 103,
    // ctype: 1, // 1测验 2小测 3考试
    // status: 1 // 进行中
  })

  if (res.code == 0) {
    console.log('exammylist', res.data)
    exammylist.value = res.data
  } else {
    showMessage('error', res.msg)
  }
}

// 打开考试概述
async function openExamSum(eid) {
  examId.value = eid
  console.log(eid);
  // 请求考试详情
  let res = await axios.post(api.examcoursedetail, { uid: uid.value, id: eid })
  console.log(res);

  if (res.code == 0) {
    examdetail.value = res.data

    // 统计题目数量和类型
    let temobj = await getExamQuestions(eid)
    examdetail.value.qnum = temobj.qnum
    examdetail.value.qtype = temobj.qtype

    dialogExamVisible.value = true
  } else {
    showMessage('error', '考卷详情加载失败：' + res.msg)
  }
}

// 跳转考试详情
function gotoExamDetail() {
  // 新页面打开
  const hrefstr = router.resolve({
    name: 'Examdetail',
    query: {
      eid: examId.value
    }
  }).href

  // 隐藏弹出框
  dialogExamVisible.value = false

  let timer = setTimeout(() => {
    clearTimeout(timer)
    window.open(hrefstr, '_blank')
  }, 300);
}

// 获取考题列表
async function getExamQuestions(pid) {

  let res = await axios.post(api.exambindlist, { pid: pid })
  console.log(res);

  let single = []   // 单选
  let multiple = [] // 多选
  let fillin = []   // 填空
  let longfill = [] // 简答
  let judge = [] // 判断

  if (res.code == 0) {
    // console.log('exammylist', res.data)
    res.data.forEach(item => {
      switch (item.ctype) {
        // 单选
        case 1:
          single.push(item)
          break;
        // 多选
        case 2:
          multiple.push(item)
          break;

        // 填空
        case 4:
          fillin.push(item)
          break;
        // 判断
        case 5:
          judge.push(item)
          break;
        // 简答
        case 6:
          longfill.push(item)
          break;
        default:
          break;
      }
    });

    let qtype = ''
    let qnum = 0

    if (single.length != 0) {
      qtype += `${single.length}选择题；`
      qnum += single.length
    }
    if (multiple.length != 0) {
      qtype += `${multiple.length}多选题；`
      qnum += multiple.length
    }
    if (judge.length != 0) {
      qtype += `${judge.length}判断题；`
      qnum += judge.length
    }
    if (fillin.length != 0) {
      qtype += `${fillin.length}填空题；`
      qnum += fillin.length
    }
    if (longfill.length != 0) {
      qtype += `${longfill.length}简答题`
      qnum += longfill.length
    }

    return { qtype, qnum }
  } else {
    showMessage('error', res.msg)
    return {}
  }
}

// 信息提示
const showMessage = (msgtype, msgstr) => {
  ElMessage({
    showClose: true,
    message: msgstr,
    type: msgtype
  })
}
</script>

<style scoped>
.coursebox {
  background-color: #f2f4f7;
  padding: 30px;
  /* padding-bottom: 0; */
  /* padding-right: 0; */
}

.innerbox {
  background-color: #fff;
  border-radius: 10px;
  padding: 10px;
  overflow: hidden;
}

.scrollbar-demo-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  margin: 10px;
  text-align: center;
  border-radius: 4px;
  background: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
}

.searchbox {
  float: right;
  display: flex;
  width: 200px;
  height: 36px;
  border: 1px solid #d4d6d9;
  font-size: 14px;
  color: #131b26;
  padding: 0 14px;
  border-radius: 40px;
  align-items: center;
  margin-right: 50px;
}

.searchbox input {
  width: 155px;
  height: 36px;
}

.searchbox:hover {
  border-color: #3a8bff;
}

.searchbox:hover .colorChange {
  color: #3a8bff !important;
}

.borderblue {
  border-color: #3a8bff;
}

.colorblue {
  color: #3a8bff !important;
}

.cursorbtn:hover {
  cursor: pointer;
}

.searchbox:focus {
  /* border-color: #3a8bff; */
  background-color: #000;
}

#searchinp:focus .colorChange {
  color: #3a8bff !important;
}

/* 课程列表具体样式 */
.courselistul {
  width: 900px;
  margin: 0 auto;
  perspective: 1000px;
}

.courseitem {
  float: left;
  box-sizing: border-box;
  position: relative;
  width: 280px;
  height: 160px;
  font-size: 12px;
  border: 1px solid #aaa;
  border-radius: 8px;
  cursor: pointer;
  margin-right: 30px;
  margin-bottom: 20px;
  box-shadow: 10px 10px 10px #ccc;
  transition: all ease 0.3s;
  /* background-color: pink; */
}

.courseitem:hover {
  transform: translateZ(10px);
  box-shadow: 10px 10px 20px #ccc;
}

.courseitem:nth-child(3n) {
  margin-right: 0px;
}

.courseitem .infobox {
  position: relative;
  box-sizing: border-box;
  padding: 0 6px;
}

.infobox .examtitle {
  width: 80%;
  height: 50px;
  line-height: 50px;
  margin: 0 auto;
  color: #131b26;
  font-weight: 600;
  border-bottom: 1px solid #000;
  text-align: center;
}

.infobox p {
  line-height: 17px;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  line-clamp: 1;
  -webkit-box-orient: vertical;
}

.middlebox {
  width: 94%;
  height: 30px;
  display: flex;
  align-items: center;
  margin: 15px auto;
}

.middlebox .examcourse {
  width: 190px;
  height: 30px;
  line-height: 30px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.infobox .duringtime {
  width: 94%;
  margin: 10px auto;
  font-size: 12px;
}
</style>

<style scoped>
.studentinfo {
  display: flex;
  width: 90%;
  height: 150px;
  margin: 0 auto;
  /* background-color: pink; */
  margin-bottom: -50px;
  padding-top: 20px;
}

.examinfo {
  width: 90%;
  height: 400px;
  margin: 0 auto;
  /* background-color: skyblue; */
  padding-top: 30px;
}

.studentinfo ul li,
.examinfo ul li {
  display: flex;
  height: 28px;
  margin: 0px auto;
  align-items: center;
  font-size: 16px;
}

li .labelbox {
  width: 80px;
  margin-left: 20px;
  margin-right: 10px;
  /* background-color: yellowgreen; */
}

li .contentbox {
  width: 360px;
  margin-right: 10px;
  /* background-color: orange; */
}

.examinfo ul li div {
  font-weight: 700 !important;
}

.btnbox {
  text-align: center;
  margin-top: 40px;
}

.btnstyle {
  width: 200px;
  margin: 0 auto;
}

.studentinfo .score {
  display: flex;
  width: 240px;
  height: 100%;
  margin-left: -60px;
  /* align-items: center; */
}

.studentinfo .score p {
  color: #0099fe;
  font-size: 30px;
  font-weight: 700;
}
</style>